var http = require('http');
var url = require('url');
var port = 8888;

function start(router, handle) {
	http.createServer(function(request, response) {
		// get the pathname
		var pathName = url.parse(request.url).pathname;

		var postData = '';

		request.setEncoding('utf-8');

		request.addListener('data', function (d) {
			postData += d;
			console.log('Received POST data chunk: ' + d + '.');
		})

		request.addListener('end', function (d) {
			request.postData = postData;
			// do route
			router(pathName, handler, request, response);
		})

	}).listen(port);
	console.log('Server started on ' + port + ', at ' + new Date());
};

exports.start = start;
